package de.unihannover.hci.portraitquiz;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {

	private Context mContext;
	private PortraitQuiz app;
	private Menu menu;
	private ImageView titleLogo;
	private Animation titleStart_anim;
	private Animation logoAnim;
	private MediaPlayer mp;
	private ImageView logoImage;
	protected Animation logoAnim2;
	private Button btn_actors;
	private Button btn_hannover96;
	private Button polit_btn;
	private Button random_btn;
	private Typeface type;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.activity_main);

		mContext = this;
		app = (PortraitQuiz) getApplication();

		// Set title font
		TextView title1 = (TextView) findViewById(R.id.title);
		TextView title2 = (TextView) findViewById(R.id.title2);
		type = app.getAppFontType();
		title1.setTypeface(type);
		title2.setTypeface(type);

		// Buttons vorbereiten
		prepareButtons();

		// Animationen vorbereiten
		prepareAnimations();

		// Sound vorbereiten
		prepadeSound();
	}

	private void prepareButtons() {
		// Creating Buttons
		btn_actors = (Button) findViewById(R.id.btn_actors);
		btn_actors.setTypeface(type);
		btn_hannover96 = (Button) findViewById(R.id.btn_hannover96);
		btn_hannover96.setTypeface(type);
		polit_btn = (Button) findViewById(R.id.politicians);
		polit_btn.setTypeface(type);
		random_btn = (Button) findViewById(R.id.random);
		random_btn.setTypeface(type);

		// RelativeLayout btn_hannover96 = (RelativeLayout)
		// findViewById(R.id.btn_hannover96);

		// +++ Handling Button Click Events +++
		final Intent gamePlayIntent = new Intent(this, GamePlayActivity.class);

		// Listening to actors button
		btn_actors.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				app.setCurrentCategory(1);
				// Start game play activity if the category contains answers
				if (app.getCurrentCategory().getAnswers().size() > 0) {
					startActivity(gamePlayIntent);
				}
			}
		});

		// Listening to Hannover96 button
		btn_hannover96.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				app.setCurrentCategory(2);
				// Start game play activity if the category contains answers
				if (app.getCurrentCategory().getAnswers().size() > 0) {
					startActivity(gamePlayIntent);
				}
			}
		});

		// Listening to Politicians button
		polit_btn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				app.setCurrentCategory(3);
				// Start game play activity if the category contains answers
				if (app.getCurrentCategory().getAnswers().size() > 0) {
					startActivity(gamePlayIntent);
				}
			}
		});

		// Listening to Random button
		random_btn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				int randomNum = (int) Math.ceil(Math.random() * 3);
				app.setCurrentCategory(randomNum);
				// Start game play activity if the category contains answers
				if (app.getCurrentCategory().getAnswers().size() > 0) {
					startActivity(gamePlayIntent);
				}
			}
		});
	}

	private void setStarsOnButtons() {
		Drawable stars = null;
		stars = this.getResources().getDrawable(app.getStarsDrawableForCategory(1));
		btn_actors.setCompoundDrawablesWithIntrinsicBounds(null, null, null, stars);
		stars = this.getResources().getDrawable(app.getStarsDrawableForCategory(2));
		btn_hannover96.setCompoundDrawablesWithIntrinsicBounds(null, null, null, stars);
		stars = this.getResources().getDrawable(app.getStarsDrawableForCategory(3));
		polit_btn.setCompoundDrawablesWithIntrinsicBounds(null, null, null, stars);
	}

	private void prepadeSound() {
		mp = MediaPlayer.create(MainActivity.this, R.raw.laugh);
		mp.setOnCompletionListener(new OnCompletionListener() {
			public void onCompletion(MediaPlayer mp) {
				// mp.release();
			}
		});
		mp.setVolume(1, 1);
		mp.setLooping(false);
	}

	private void prepareAnimations() {
		// Animationen vorbereiten
		titleLogo = (ImageView) findViewById(R.id.titleLogo);
		titleStart_anim = AnimationUtils.loadAnimation(this, R.anim.titlelogo_anim);
		
		//Titellogo kitzeln
		logoAnim = AnimationUtils.loadAnimation(this, R.anim.laugh_anim);
		logoAnim2 = AnimationUtils.loadAnimation(this, R.anim.laugh_anim2);
		logoAnim.setAnimationListener(new AnimationListener() {
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub

			}

			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub

			}

			public void onAnimationEnd(Animation animation) {
				CountDownTimer ct1 = new CountDownTimer(300, 1000) {
					@Override
					public void onTick(long millisUntilFinished) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onFinish() {
						logoImage.startAnimation(logoAnim2);
					}
				};
				ct1.start();
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		this.menu = menu;
		return true;
	}

	@SuppressLint("NewApi")
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		// case R.id.filter:
		// �ffne untere ActionBar mit Filterung
		// break;
		case R.id.menu_choose_obstacle:
			final Intent obstacleChoserIntent = new Intent(this, ObstacleChooserActivity.class);
			startActivity(obstacleChoserIntent);
			break;
		case R.id.playSounds:
			MenuItem playItem = menu.findItem(R.id.playSounds);
			if (playItem.getTitle().equals("Muted")) {
				playItem.setTitle("Unmuted");
				playItem.setIcon(R.drawable.volume_on);
				app.setPlaySounds(true);

			} else {
				playItem.setTitle("Muted");
				playItem.setIcon(R.drawable.volume_muted);
				app.setPlaySounds(false);
			}
			break;
		case R.id.about:
			LayoutInflater li = LayoutInflater.from(this);
			View about_view = li.inflate(R.layout.info_dialog_layout, null);
			final AlertDialog.Builder builder = (android.os.Build.VERSION.SDK_INT < 11) ? new AlertDialog.Builder(this)
					: new AlertDialog.Builder(this, AlertDialog.THEME_HOLO_DARK);
			builder.setTitle("About");
			builder.setView(about_view);
			builder.setIcon(R.drawable.about);
			builder.setCancelable(true);
			builder.setPositiveButton("Cancel", new DialogInterface.OnClickListener() {

				public void onClick(DialogInterface dialog, int which) {
					dialog.cancel();
					titleLogo.startAnimation(titleStart_anim);
				}
			});
			AlertDialog dialog = builder.create();
			dialog.show();
			break;
		}
		return true;
	}

	public void onLogoClick(View view) {
		logoImage = (ImageView) view;
		logoImage.startAnimation(logoAnim);

		if (app.playsSounds()) {
			try {
				if (mp.isPlaying()) {
					mp.seekTo(0);
				}
				mp.start();
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	@Override
	public void onStart() {
		super.onStart();
		setStarsOnButtons();
		titleLogo.startAnimation(titleStart_anim);
	}
}
